const mysql = require('mysql2/promise');

// 数据库连接配置
const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'chatime',
  port: 3306
};

async function testAvatars() {
  const pool = mysql.createPool(dbConfig);
  
  try {
    console.log('🔗 连接数据库...');
    const connection = await pool.getConnection();
    console.log('✅ 数据库连接成功');
    
    // 测试用户登录
    console.log('\n1️⃣ 测试用户登录...');
    const [users] = await connection.execute(
      'SELECT * FROM users WHERE id = ?',
      ['user1']
    );
    
    if (users.length === 0) {
      console.log('❌ 测试用户不存在');
      return;
    }
    
    const testUser = users[0];
    console.log('✅ 用户登录成功');
    console.log('👤 用户ID:', testUser.id);
    console.log('👤 用户头像:', testUser.avatar);
    
    // 测试聊天列表头像
    console.log('\n2️⃣ 测试聊天列表头像...');
    const [chatMessages] = await connection.execute(`
      SELECT DISTINCT 
        c.id,
        c.sender_id,
        c.receiver_id,
        c.content,
        c.type,
        c.timestamp,
        c.is_read,
        u.name as sender_name,
        u.avatar as sender_avatar
      FROM chat_messages c
      LEFT JOIN users u ON c.sender_id = u.id
      WHERE c.sender_id = ? OR c.receiver_id = ?
      ORDER BY c.timestamp DESC
      LIMIT 5
    `, [testUser.id, testUser.id]);
    
    console.log('📊 聊天消息数量:', chatMessages.length);
    chatMessages.forEach((msg, index) => {
      console.log(`  消息${index + 1}:`);
      console.log(`    发送者: ${msg.sender_name}`);
      console.log(`    头像: ${msg.sender_avatar}`);
      console.log(`    内容: ${msg.content.substring(0, 20)}...`);
    });
    
    // 测试好友列表头像
    console.log('\n3️⃣ 测试好友列表头像...');
    const [friends] = await connection.execute(`
      SELECT 
        f.id,
        f.user_id,
        f.friend_id,
        u.name as friend_name,
        u.avatar as friend_avatar,
        u.signature as friend_signature,
        f.add_time,
        0 as is_online
      FROM friends f
      LEFT JOIN users u ON f.friend_id = u.id
      WHERE f.user_id = ?
    `, [testUser.id]);
    
    console.log('📊 好友数量:', friends.length);
    friends.forEach((friend, index) => {
      console.log(`  好友${index + 1}:`);
      console.log(`    姓名: ${friend.friend_name}`);
      console.log(`    头像: ${friend.friend_avatar}`);
      console.log(`    签名: ${friend.friend_signature}`);
    });
    
    connection.release();
    console.log('\n🎉 头像数据测试完成！');
    
  } catch (error) {
    console.error('❌ 测试失败:', error);
  } finally {
    await pool.end();
  }
}

testAvatars(); 